﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using System.Diagnostics;
using System.Collections;
using System.Data;
using plust.Persistencia;
using System.Threading;
using System.Collections.ObjectModel;
using FirstFloor.ModernUI.Windows.Controls;
using System.ComponentModel;
using FirstFloor.ModernUI.Windows;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for EliminarProducto.xaml
    /// </summary>
    public partial class EliminarProducto : UserControl, IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public EliminarProducto()
        {
            InitializeComponent();
        }

        private async void cargarProductos()
        {
            this.pb.IsIndeterminate = true;

            dgEliminarProductos.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarProductosTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            dgEliminarProductos.ItemsSource = slowTask.Result.DefaultView;

            dgEliminarProductos.Visibility = System.Windows.Visibility.Visible;

            this.pb.IsIndeterminate = false;
        }

        private DataTable cargarProductosTask()
        {
            return persistencia.obtenerProductos();
        }

        private void dgEliminarProductos_Row_DoubleClick(object sender, RoutedEventArgs e)
        {
            if (this.dgEliminarProductos.SelectedItem != null)
            {
                MessageBoxButton btn = MessageBoxButton.YesNo;
                
                MessageBoxResult mbs =  ModernDialog.ShowMessage("¿Realmente desea eliminar este producto del sistema?", "Eliminar Producto", btn);

                if(mbs == MessageBoxResult.Yes)
                {
                    if (this.persistencia.eliminarProducto(((DataRowView)dgEliminarProductos.SelectedItem).Row["id_producto"].ToString()))
                    {
                        new ModernDialog
                        {
                            Title = "CORRECTO",
                            Content = "Producto eliminado.",
                        }.ShowDialog();                    
                    }
                    else
                    {
                        new ModernDialog
                        {
                            Title = "ERROR",
                            Content = "Se ha producido un error inesperado.",
                        }.ShowDialog();
                    }

                    this.cargarProductos();
                }      
            }
        }


        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            cargarProductos();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            
        }
    }
}
